<template>
  <div class="pages warehouseView">
    <div class="pageLevel">
      <!-- <div class="head">
        <breadcrumb></breadcrumb>
      </div> -->
      <div class="operationBox tableSearch">
        <div class="checkbox">
          <el-checkbox v-model="status" label="0" :disabled="type==1"> 启用</el-checkbox>
        </div>
        <el-button type="primary" @click="goBack()" size="mini">返回</el-button>
        <el-button v-if='type==1&&setRole("修改")' type="primary"  @click="addmodify()" size="mini">修改</el-button>
        <el-button v-if='type==0' type="primary" @click="submitForm('formdata')" size="mini">保存</el-button>
      </div>
      <div class="paymentfrom">
        <el-form :model="ruleFormza" :rules="rulesza" ref="formdata" label-width="100px" class="demo-ruleForm">
          <el-form-item label="仓库编码" prop="code" disabled v-if="id">
            <el-input v-model="ruleFormza.code" size="small" disabled></el-input>
          </el-form-item>
          <el-form-item label="仓库名称" prop="repositoryName">
            <el-input v-model="ruleFormza.repositoryName" size="small" :disabled="type==1"></el-input>
          </el-form-item>
          <el-form-item label="详细地址" prop="repositoryAddress">
            <el-input v-model="ruleFormza.repositoryAddress" size="small" :disabled="type==1"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import util from "@/util";
import {
  saveRepositoryWarehouse,
  updateRepositoryWarehouse,
  selectRepositoryDetails
} from "@/api";
export default {
  data() {
    return {
      disable: false,
      data: {},
      show: "",
      id: "",
      type: "",
      status: true,
      repositoryId: "",
      ruleFormza: {
        code: "",
        repositoryName: "",
        repositoryAddress: ""
      },
      parentIdoptions: [],
      bankId: "",
      rulesza: {
        code: [{ required: true, message: "请填写仓库编码", trigger: "blur" }],
        repositoryName: [
          { required: true, message: "请填写仓库名称", trigger: "blur" }
        ],
        repositoryAddress: [
          { required: true, message: "请填写详细地址", trigger: "blur" }
        ]
      }
    };
  },
  computed: mapState(["merchantId", "pageLevel", "path","power"]),
  mounted() {
    this.type = util.getQuery("type");

    if (this.type == 1) {
      this.id = util.getQuery("id");
      this.status = util.getQuery("status") == 1 ? false : true;
      this.getdetails(this.id);
    }

    // this.path.pop();
    // if (this.data.id) {
    //   this.ruleFormza.code = this.data.code;
    //   this.ruleFormza.repositoryName = this.data.repositoryName;
    //   this.ruleFormza.repositoryAddress = this.data.repositoryAddress;
    //   this.id = this.data.id;
    //   this.disable = true;
    //   // this.status = this.data.status;
    // } else {

    // }
  },
  methods: {
     //配置权限
    setRole(key) {
      return util.selectTitle(this.power, key);
    },
    goBack() {
      history.back();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.preservation();
        } else {
          return false;
        }
      });
    },
    getdetails(id) {
      let data = {
        repositoryId: id
      };
      selectRepositoryDetails(data).then(res => {
        if (res.data.status == 0) {
          res.data.status = true;
        } else {
          res.data.status = false;
        }
        this.ruleFormza = res.data;
      });
    },
    //保存
    preservation() {
      let data = {
        warehouse: {
          repositoryName: this.ruleFormza.repositoryName,
          code: this.ruleFormza.code,
          repositoryAddress: this.ruleFormza.repositoryAddress,
          status: this.status ? 0 : 1,
          shopId: this.merchantId
        }
      };
      if (this.id != "") {
        data.warehouse.id = this.id;
        updateRepositoryWarehouse(data).then(res => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: res.msg
            });
            this.$store.commit("SET_ISRELOADDATA", true);
            window.history.back();
            return false;
          }
        });
      } else {
        saveRepositoryWarehouse(data).then(res => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: res.msg
            });
            history.go(-1);
            return false;
          }
        });
      }
    },
    //修改
    addmodify() {
      this.type = 0;
    }
  },
  components: {
    // breadcrumb
  }
};
</script>

<style scoped>
/* 保存的样式 */
.tableSearch {
  margin-left: 10px;
}
.paymentfrom {
  margin: 20px 10px 0 0;
}
.paymentfrom .el-input {
  width: 300px;
}

.rowtwo .el-select {
  width: 300px;
}
</style>
